<?php

// +----------------------------------------------------------------------
// | Apprh.Shop
// +----------------------------------------------------------------------
// | Copyright (c) 2017-2027 http://www.apprh.com All rights reserved.
// +----------------------------------------------------------------------
// | Notice: This code is not open source, it is strictly prohibited
// |         to distribute the copy, otherwise it will pursue its
// |         legal responsibility.
// +----------------------------------------------------------------------
// | Author: 雲溪荏苒 <290648237@qq.com>
// +----------------------------------------------------------------------

namespace app\admin\controller;

use controller\BasicAdmin;
use service\DataService;
use service\ToolsService;
use think\Db;

/**
 * 视频管理
 * Video
 * @package app\admin\controller
 * @author 雲溪荏苒 <290648237@qq.com>
 * @date 2017/12/16
 */
class Video extends BasicAdmin
{

    /**
     * 指定当前数据表
     * @var string
     */
    public $table = 'Video';

//    public $productClassTable = 'VideoClass';


    public function index()
    {

        $this->title = '视频管理';
        $get = $this->request->get();
        $db = Db::name($this->table)->order('sort asc, id asc');

        foreach (['v_name'] as $key) {
            if (isset($get[$key]) && $get[$key] !== '') {
                $db->where($key, 'like', "%{$get[$key]}%");
            }
        }

        // 视频分类名称
        /*$data = parent::_list($db, true, false);
        foreach($data['list'] as $key=>$list) {
            $data['list'][$key]['class_name'] = Db::name($this->productClassTable)->where('id', $list['cid'])->value('class_name');
        }

        return $this->fetch('', $data);*/
        return parent::_list($db);
    }

    /**
     * 添加
     */
    public function add() {
        return $this->_form($this->table, 'form');
    }

    /**
     * 编辑
     */
    public function edit() {
        return $this->add();
    }

    /**
     * 表单数据前缀方法
     * @param array $vo
     */
    protected function _form_filter(&$vo)
    {
        if ($this->request->isGet()) {
            // 上级菜单处理
            /*$_menus = Db::name($this->productClassTable)->where(['status' => '1'])->order('sort asc,id asc')->select();
            $_menus[] = ['class_name' => '顶级菜单', 'id' => '0', 'pid' => '-1'];
            $menus = ToolsService::arr2table($_menus);
            foreach ($menus as $key => &$menu) {
                if (substr_count($menu['path'], '-') > 3) {
                    unset($menus[$key]);
                    continue;
                }
                if (isset($vo['pid'])) {
                    $current_path = "-{$vo['pid']}-{$vo['id']}";
                    if ($vo['pid'] !== '' && (stripos("{$menu['path']}-", "{$current_path}-") !== false || $menu['path'] === $current_path)) {
                        unset($menus[$key]);
                    }
                }
            }
            $this->assign('menus', $menus);*/
        }
    }

    /**
     * 删除
     */
    public function del() {
        if (DataService::update($this->table)) {
            $this->success("删除成功！", '');
        }
        $this->error("删除失败，请稍候再试！");
    }

    /**
     * 禁用
     */
    public function forbid() {
        if (DataService::update($this->table)) {
            $this->success("操作成功！", '');
        }
        $this->error("操作失败，请稍候再试！");
    }

    /**
     * 启用
     */
    public function resume() {
        if (DataService::update($this->table)) {
            $this->success("操作成功！", '');
        }
        $this->error("操作失败，请稍候再试！");
    }

    /**
     * 取消推荐
     */
    public function cancel_recommend() {
        if (DataService::update($this->table)) {
            $this->success("操作成功！", '');
        }
        $this->error("操作失败，请稍候再试！");
    }

    /**
     * 推荐
     */
    public function recommend() {
        if (DataService::update($this->table)) {
            $this->success("操作成功！", '');
        }
        $this->error("操作失败，请稍候再试！");
    }

    public function _form_result($result) {
        $result !== false ? $this->success('恭喜，保存成功哦！', url('/').'admin.html#'.url('index').'?spm='.$this->request->param('spm')) : $this->error('保存失败，请稍候再试！');
    }
}
